using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace teatimer
{
    public class INIFILE
    {
        // Um Werte in aus der INI zu lesen                                                             
        public static String ReadSetting(String Filename, String Section, String Entry, String Default)
        {
            if (File.Exists(Filename))
            {
                StreamReader reader = new StreamReader(Filename);
                String line = "";
                String toreturn = Default;
                bool section = false;
                while ((line = reader.ReadLine()) != null)
                {
                    if (!line.StartsWith(";"))
                    {
                        if (line.StartsWith("[") && line.EndsWith("]"))
                        {
                            if (line == ("[" + Section + "]"))
                                section = true;
                            else
                                section = false;
                        }
                        if (section)
                        {
                            if (line.StartsWith(Entry + "="))
                            {
                                if (line.Length >= Entry.Length + 2)
                                {
                                    toreturn = line.Substring(Entry.Length + 1);
                                    break;
                                }
                                else
                                {
                                    toreturn = "";
                                    break;
                                }
                            }
                        }
                    }
                }
                reader.Close();
                return toreturn;
            }
            else
            {
                throw new IOException("File not found");
            }
        }

        // Um werte in die INI zu schrieben                                                         
        public static void WriteSetting(String Filename, String Section, String Entry, String Value)
        {
            StreamWriter writer = new StreamWriter("~" + Filename, false);
            bool section = false;
            bool found = false;
            if (File.Exists(Filename))
            {
                String line = "";
                StreamReader reader = new StreamReader(Filename);
                while ((line = reader.ReadLine()) != null)
                {
                    if (!line.StartsWith(";"))
                    {
                        if (line.StartsWith("[") && line.EndsWith("]"))
                        {
                            if (section && !found)
                            {
                                writer.WriteLine(Entry + "=" + Value);
                                found = true;
                            }
                            writer.WriteLine(line);
                            if (!section && line == "[" + Section + "]")
                            {
                                section = true;
                            }
                            else
                            {
                                section = false;
                            }
                        }
                        else
                        {
                            if (!found)
                            {
                                if (section && line.StartsWith(Entry + "="))
                                {
                                    writer.WriteLine(Entry + "=" + Value);
                                    found = true;
                                }
                                else
                                {
                                    writer.WriteLine(line);
                                }
                            }
                            else
                            {
                                writer.WriteLine(line);
                            }
                        }
                    }
                    else
                    {
                        writer.WriteLine(line);
                    }
                }
                reader.Close();
            }
            if (!found)
            {
                if (!section)
                    writer.WriteLine("[" + Section + "]");
                writer.WriteLine(Entry + "=" + Value);
            }
            writer.Close();
            try { File.Delete(Filename); }
            catch { }
            File.Move("~" + Filename, Filename);
        }
    }


}
